package com.competition.front.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.competition.commonutils.R;
import com.competition.front.entity.*;
import com.competition.front.entity.vo.ScoreVo;
import com.competition.front.mapper.ExaminationDetailMapper;
import com.competition.front.service.ExaminationDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.competition.front.service.ExaminationService;
import com.competition.front.service.UserAdditionalService;
import com.competition.front.service.UserCompetitionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 知识竞赛题目表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-03-30
 */
@Service
public class ExaminationDetailServiceImpl extends ServiceImpl<ExaminationDetailMapper, ExaminationDetail> implements ExaminationDetailService {
    @Autowired
    private ExaminationService examinationService;
    @Autowired
    private UserCompetitionService userCompetitionService;
    @Autowired
    private UserAdditionalService userAdditionalService;
    //统计分数
    @Override
    public R statisticalScore(ScoreVo scoreVo, String userId, R result) {
        //根据编号获取该试题
        Examination examination = examinationService.getById(scoreVo.getExaminationId());
        //根据试题的总数题目数算出每道题分数
        Integer topicScore = examination.getTopicScore();
        Integer topicNum = examination.getTopicNum();
        Integer score=topicScore/topicNum;
        System.out.println("+++++++++++++++++++++++++++++++++++++++++"+score);
        //记录总分
        Integer resultScore=0;
        //获取用户填入的分数
        Map<String,String> userMap=scoreVo.getUserAnswer();
        List<Integer> ret=new ArrayList<>();
        //根据试题编号查出试题的所有答案
        QueryWrapper<ExaminationDetail> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("examination_id",scoreVo.getExaminationId());
        //获取所有有关试题
        List<ExaminationDetail> examinationDetails = baseMapper.selectList(queryWrapper);
        //根据每个试题编号,在map中寻找相对的答案检验
        for (ExaminationDetail examinationDetail:examinationDetails){
            String answer=userMap.get(examinationDetail.getId());
            if(answer!=null){
                if(answer.equals(examinationDetail.getAnswer())) {
                    ret.add(1);
                    resultScore=resultScore+score;

                    System.out.println("=========================================="+resultScore);


                }else{
                    ret.add(0);
                }
            }
        }
        //添加竞赛记录
        //插入用户分数
        UserCompetition userCompetition=new UserCompetition();
        userCompetition.setUserId(userId);
        userCompetition.setExaminationId(scoreVo.getExaminationId());
        userCompetition.setScore(resultScore);
        userCompetitionService.save(userCompetition);
        //更新用户总分
        QueryWrapper<UserAdditional> userAdditionalQueryWrapper=new QueryWrapper<>();
        userAdditionalQueryWrapper.eq("user_id",userId);
        if(userAdditionalService.list(userAdditionalQueryWrapper).size()==0){
            UserAdditional userAdditional=new UserAdditional();
            userAdditional.setUserId(userId);
            userAdditional.setTotalScore(resultScore);
            userAdditional.setEssayNumber(0);
            userAdditionalService.save(userAdditional);
        }else {
            UserAdditional userAdditional = userAdditionalService.list(userAdditionalQueryWrapper).get(0);
            UpdateWrapper<UserAdditional> userAdditionalUpdateWrapper=new UpdateWrapper<>();
            userAdditionalUpdateWrapper.eq("user_id",userAdditional.getUserId());
            //1.获取用户总分
            Integer totalScore = userAdditional.getTotalScore();
            totalScore=totalScore+resultScore;
            //2.更新用户总分
            userAdditional.setTotalScore(totalScore);
            userAdditionalService.update(userAdditional,userAdditionalUpdateWrapper);
        }
        //返回结果
        return result.data("score",resultScore).data("resultList",ret).data("userMap",userMap);
    }
    @Override
    public List<ExaminationDetail> getExaminationSubject(String examinationId) {
        QueryWrapper<ExaminationDetail> examinationDetailQueryWrapper=new QueryWrapper<>();
        examinationDetailQueryWrapper.eq("examination_id",examinationId);
        List<ExaminationDetail> examinationDetails = baseMapper.selectList(examinationDetailQueryWrapper);
        return examinationDetails;
    }
}
